package com.mengtian_tech.tju.dataquery.api.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.util.StringUtil;
import com.mengtian_tech.tju.dataquery.api.service.AskLeaveService;
import com.mengtian_tech.tju.dataquery.api.service.AsyncDateService;
import com.mengtian_tech.tju.dataquery.api.util.ResponseUtil;

@RestController
@RequestMapping("/api/askleave")
public class AskLeaveController {
	
	@Autowired
	@Qualifier("jdbcTemplateDataAi")
	JdbcTemplate jdbcTemplateDataAi;
	
	@Autowired
	AskLeaveService askLeaveService;

	@Autowired
	AsyncDateService asyncDateService;
	

	@RequestMapping("rpt")
	public Object rpt(String begin,String end,String level) throws ParseException {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date beginDate = sdf.parse(begin);
		Date endDate = sdf.parse(end);
		
		//获取请假数量
		String sql1 = "select * from ask_leave_rpt_count where date_on >=? and date_on <= ?";
		List<Map<String,Object>> list = this.jdbcTemplateDataAi.queryForList(sql1
				,begin,end);
		
		//统计任务类型
		List<Object> params =new ArrayList<Object>();
		String sql2 = " select task_type,count(1) cnt from ask_leave_log  where ("
				+ " (ask_leave_from <= ? and ask_leave_to >= ?) or "
				+ " (ask_leave_from >= ? and ask_leave_to <= ?) or"
				+ " (ask_leave_from <= ? and ask_leave_to >= ?) )";
			
		params.add(beginDate);
		params.add(beginDate);
		params.add(beginDate);
		params.add(endDate);
		params.add(endDate);
		params.add(endDate);
		
		//判断是否有任务类型
		if(StringUtil.isNotEmpty(level)) {
			sql2 += " and post_level = ? ";
			params.add(level);
		}
		
		sql2 += "  group by task_type ";		
		
		
		List<Map<String,Object>> list2 = this.jdbcTemplateDataAi.queryForList(sql2,params.toArray());
		
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("rpt1", list);
		result.put("rpt2", list2);
		return ResponseUtil.ok(result);
	}
	
	@RequestMapping("dataList")
	public Object dataList(Integer pageNo,String key,String begin,String end,String taskType,String postLevel) throws ParseException {
	 
		//列表
		List<Map<String,Object>> list =  askLeaveService.getLeaveList(pageNo, key, begin, end, taskType, postLevel,20);
		
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("list", list);
		
		//最近同步日期
		Date asynDate = this.asyncDateService.getAsyncDate("02");
		result.put("asynDate", asynDate);

		
		return ResponseUtil.ok(result);
	}
	
	
	@RequestMapping("dataListById")
	public Object dataListById(Integer pageNo,String key,String begin,String end) throws ParseException {
		int pageSize = 20;
		Map<String,Object> result = new HashMap<String, Object>();
		
		String sql = "select * from ask_leave_log  where 1=1 ";
		List<Object> params =  new ArrayList<Object>();
		if(StringUtil.isNotEmpty(key)) {
			params.add(key);			
			sql += " and  (user_id=?)";
			
		}
	 
		 
		//排序和分页
		sql +=  " order by ask_leave_from desc , id desc limit ?,?";
		params.add((pageNo-1)*pageSize);
		params.add(pageSize);
		
	 
		//列表
		List<Map<String,Object>> list = this.jdbcTemplateDataAi.queryForList(
				sql,params.toArray());

		result.put("list", list);

		//最近同步日期
		Date asynDate = this.asyncDateService.getAsyncDate("02");
		result.put("asynDate", asynDate);
		
		return ResponseUtil.ok(result);
	}
	
}
